package com.loong.life.controller.forehead;

import com.loong.common.mvc.BaseForeheadController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonValidate;
import com.loong.life.common.forehead.param.PhoneChargeRecordForeheadParam;
import com.loong.life.common.forehead.param.PhoneChargeQueryForeheadParam;
import com.loong.life.handler.PhoneChargeHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @Description: 处理前台有关手机充值请求的controller
 * @Author: 刘群
 * @Date: 2020/11/26 16:37
 */
@Slf4j
@RestController
@RequestMapping("forehead/life/phoneCharge")
public class PhoneChargeForeheadController extends BaseForeheadController {

    @Resource
    private PhoneChargeHandler phoneChargeHandler;

    /**
     * 创建手机充值订单
     * @param param 手机充值记录参数
     * @return 响应的结果
     */
    @PostMapping("createOrder")
    public Result createPhoneChargeOrder(@RequestBody @Validated({CommonInsert.class}) PhoneChargeRecordForeheadParam param){
        param.setId(null);
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> phoneChargeHandler.foreheadCreatePhoneChargeOrder(param));
    }

    /**
     * 取消订单
     * @param param 手机充值记录id
     * @return 响应的结果
     */
    @PostMapping("cancelOrder")
    public Result cancelPhoneChargeOrder(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> phoneChargeHandler.foreheadCancelPhoneChargeOrder(param.getId()));
    }

    /**
     * 根据id查找手机充值记录
     * @param param 充值记录id
     * @return 响应的结果
     */
    @PostMapping("findById")
    public Result findPhoneChargeRecordById(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> phoneChargeHandler.foreheadFindPhoneChargeRecordById(param.getId()));
    }

    /**
     * 分页查询手机充值记录
     * @param param  查询条件
     * @return 响应的结果
     */
    @PostMapping("pageQuery/chargeRecordList")
    public Result pageQueryPhoneChargeRecord(@RequestBody PhoneChargeQueryForeheadParam param){
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> phoneChargeHandler.foreheadPageQueryPhoneChargeRecord(param));
    }

    //TODO 缺少用户付款接口

    /**
     * 付款成功之后进行充值
     * @param param 充值参数
     * @return 响应的结果
     */
    @PostMapping("charge")
    public Result paidSuccessThenPhoneCharge(@RequestBody @Validated({CommonValidate.class}) PhoneChargeRecordForeheadParam param){
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> phoneChargeHandler.foreheadPaidSuccessThenPhoneCharge(param));
    }




}
